﻿
Partial Class WebForm_ManageMOIExport_VBasicDataControl
    Inherits System.Web.UI.UserControl

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            'Me.BindData()
       
        End If
  
    End Sub


    Protected Sub gv_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles gv.PageIndexChanging
        Me.gv.PageIndex = e.NewPageIndex
        Me.BindGV()
    End Sub
    Public Sub BindGV()

        Dim sql As New MyStringBuilder
        Dim dat As New clsData
        sql.Length = 0
        sql.AppendFormat("Select Distinct v.VolunteerID,v.VolunteerName,'1' as Nationality,'' as Detail,'' as OtherDes,'N' as YearB, ")
        sql.AppendFormat("right('0'+CAST((YEAR(BIRTHDAY)-1911) AS VARCHAR)+RIGHT(CONVERT(VARCHAR(12),BIRTHDAY,112),4),7) as Birthday ")
        sql.AppendFormat(",a.MOIValue as Gender,'N' as IsAboriginal,'' as MerriedStatus ")
        sql.AppendFormat(",'0340' as ServiceItem1,'' as ServiceItem2,'' as ServiceItem3 ")
        sql.AppendFormat(",'99' as Telent1,'' as Telent2,'' as Telent3 ")
        sql.AppendFormat(",right('000000'+cast( (SUBSTRing(v.VolunteerID,9,10)*10000+Month(v.Birthday)*100+Day(v.Birthday)) as varchar),6)  as Newpassword ")
        sql.AppendFormat(",'N' as Relief,b.MOIValue as OccupationType,c.MOIValue as EducationLevel,v.Email as Mail, vi.Parent,'6500000000' as CommunicationCode ")
        sql.AppendFormat(",'' as TCode,'' as Vcode,'' as Ncode,'' as Road,v.Address,v.OfficePhone,v.MobilePhone,'' as OfficePhone2,'' as MobilePhone2,'' as SelfIntroduction ")
        sql.AppendFormat(",'' as FirstServiseDate,v.Comment,av.IsAddTeam ")

        sql.AppendFormat(",right('0'+CAST((YEAR(v.CreateDate )-1911) AS VARCHAR)+RIGHT(CONVERT(VARCHAR(12),v.CreateDate ,112),4),7) as VTeamWorkStartTime ")
        sql.AppendFormat(",'' as VolID,'' as Title,case when cm.MOIValue<>'' then cm.MOIValue else 'V' end as VounteerType ")
        sql.AppendFormat(",'' as StartWorkTime,'' as EndWorkTime,'' as ServiceTime ")

        sql.AppendFormat("from Volunteer v ")
        sql.AppendFormat("left join OtherOrganizationMapping o on v.VolunteerID = o.UserID ")
        sql.AppendFormat("left join Codemapping cm on o.Source = cm.Value and cm.tablename = 'volunteer' and cm.columnName = 'VolunteerType' ")
        sql.AppendFormat("left join Village vi on vi.RegionID = v.VillageID ")
        sql.AppendFormat("  left JOIN (select vm.VOrganizationID,vo.VOrganizationName,vm.VolunteerID,vo.DirectUOrganizationID from VOrganizationMapping vm ")
        sql.AppendFormat("inner join VOrganizationViewV2 vo on vm.VOrganizationID=vo.VOrganizationID ) oo on v.VolunteerID=oo.VolunteerID  ")
        sql.AppendFormat("left join Codemapping a on   v.Gender=a.Value and a.TableName='Volunteer' and a.columnName='Gender' ")
        sql.AppendFormat("left join Codemapping b on  v.OccupationType=b.Value and b.TableName='Volunteer' and b.columnName='OccupationType' ")
        sql.AppendFormat("left join Codemapping c on v.EducationLevel=c.value and c.TableName='Volunteer' and c.columnName='EducationLevel' ")
        sql.AppendFormat("left join AddVOrganizationStatus av on v.VolunteerID=av.VolunteerID ")
        sql.AppendFormat("where 1=1 ")
        'If Me.Region1ID <> "" Then
        '    sql.AppendFormat(" And vi.Parent='{0}' ", Me.Region1ID)
        'End If

        'If Me.Region2ID <> "" Then
        '    sql.AppendFormat(" And v.VillageID='{0}' ", Me.Region2ID)
        'End If
        If Me.OrganizationID <> "0" Then
            sql.AppendFormat("and oo.DirectUOrganizationID='{0}' ", Me.OrganizationID)
        End If

        If Me.VOrganizationID <> "0" Then
            sql.AppendFormat(" And oo.VOrganizationID='{0}' ", Me.VOrganizationID)
        End If

        If Me.VolunteerID <> "" Then
            sql.AppendFormat(" And v.VolunteerID='{0}' ", Me.VolunteerID)
        End If

        If Me.VolunteerName <> "" Then
            sql.AppendFormat(" And v.VolunteerName Like '%{0}%' ", Me.VolunteerName)
        End If

        VBasicDataDT = dat.GetData(sql)
        For Each dr As Data.DataRow In VBasicDataDT.Rows
            If dr("OfficePhone").ToString = "0" OrElse dr("OfficePhone").ToString.Contains("e") = True OrElse dr("OfficePhone").ToString.Length < 8 Then
                dr("OfficePhone") = "29603456"
            End If

            If dr("OfficePhone").ToString.Contains("-") = True Then
                dr("OfficePhone") = dr("OfficePhone").ToString.Replace("-", "")
            End If

            If dr("OfficePhone").ToString.Contains("＃") = True OrElse dr("OfficePhone").ToString.Contains("#") = True Then
                dr("OfficePhone") = dr("OfficePhone").ToString.Replace("＃", "").Replace("#", "")
            End If
            'MobilePhone
            If dr("MobilePhone").ToString = "0" OrElse dr("MobilePhone").ToString.Contains("e") = True OrElse dr("MobilePhone").ToString.Length < 8 Then
                dr("MobilePhone") = "29603456"
            End If

            If dr("MobilePhone").ToString.Contains("-") = True Then
                dr("MobilePhone") = dr("MobilePhone").ToString.Replace("-", "")
            End If

            If dr("MobilePhone").ToString.Contains("＃") = True OrElse dr("MobilePhone").ToString.Contains("#") = True Then
                dr("MobilePhone") = dr("MobilePhone").ToString.Replace("＃", "").Replace("#", "")
            End If
        Next

        gv.DataSource = VBasicDataDT.DefaultView
        gv.DataBind()
    End Sub
    Protected Function WorkTime(ByVal VolunteerID As String) As String

        Dim sql As New MyStringBuilder
        Dim dat As New clsData
        sql.Length = 0
        sql.AppendFormat("select vkt.* from VolunteerWorkTime vkt ")
        sql.AppendFormat("left join Volunteer v  on  vkt.VolunteerID = v.VolunteerID ")
        sql.AppendFormat("where vkt.VolunteerID='{0}' ", VolunteerID)

        WorkTimeDT = dat.GetData(sql)

        strWorkTime = ""
        For i As Integer = 0 To WorkTimeDT.Rows.Count - 1

            Select Case WorkTimeDT.Rows(i)("WorkTime")
                Case "A"
                    strWorkTime += WorkTimeOK(i, "1")
                Case "N"
                    strWorkTime += WorkTimeOK(i, "2")
                Case "P"
                    strWorkTime += WorkTimeOK(i, "3")
            End Select

        Next

        If strWorkTime.Length > 0 Then
            strWorkTime = strWorkTime.Substring(0, strWorkTime.Length - 1)
        End If

        Return strWorkTime
    End Function
    Protected Function WorkTimeOK(ByVal i As Integer, ByVal time As String) As String
        ServiceTime = ""

        If WorkTimeSelect(i, "Sunday", 0) <> "" Then
            ServiceTime += WorkTimeSelect(i, "Sunday", 0) & String.Format("{0},", time)
        End If

        If WorkTimeSelect(i, "Monday", 1) <> "" Then
            ServiceTime += WorkTimeSelect(i, "Monday", 1) & String.Format("{0},", time)
        End If

        If WorkTimeSelect(i, "Tuesday", 2) <> "" Then
            ServiceTime += WorkTimeSelect(i, "Tuesday", 2) & String.Format("{0},", time)
        End If

        If WorkTimeSelect(i, "Wednesday", 3) <> "" Then
            ServiceTime += WorkTimeSelect(i, "Wednesday", 3) & String.Format("{0},", time)
        End If

        If WorkTimeSelect(i, "Thursday", 4) <> "" Then
            ServiceTime += WorkTimeSelect(i, "Thursday", 4) & String.Format("{0},", time)
        End If

        If WorkTimeSelect(i, "Friday", 5) <> "" Then
            ServiceTime += WorkTimeSelect(i, "Friday", 5) & String.Format("{0},", time)
        End If

        If WorkTimeSelect(i, "Saturday", 6) <> "" Then
            ServiceTime += WorkTimeSelect(i, "Saturday", 6) & String.Format("{0},", time)
        End If

        Return ServiceTime
    End Function
    Protected Function WorkTimeSelect(ByVal i As Integer, ByVal Week As String, ByVal num As Integer) As String
        ChooseWorkTime = ""

        If WorkTimeDT.Rows(i)(String.Format("{0}WorkTime", Week)) = "Y" Then
            ChooseWorkTime = String.Format("{0}_", num)
        End If

        Return ChooseWorkTime
    End Function

#Region "Property"
    Public Property ChooseWorkTime() As String
        Get
            If ViewState("ChooseWorkTime") Is Nothing Then
                Dim strWKtime As String = ""
                ViewState("ChooseWorkTime") = strWKtime
            End If
            Return ViewState("ChooseWorkTime")
        End Get
        Set(ByVal value As String)
            ViewState("ChooseWorkTime") = value
        End Set
    End Property

    Public Property strWorkTime() As String
        Get
            If ViewState("strWorkTime") Is Nothing Then
                Dim strWork As String = ""
                ViewState("strWorkTime") = strWork
            End If
            Return ViewState("strWorkTime")
        End Get
        Set(ByVal value As String)
            ViewState("strWorkTime") = value
        End Set
    End Property

    Public Property ServiceTime() As String
        Get
            If ViewState("ServiceTime") Is Nothing Then
                Dim strServiceTime As String = ""
                ViewState("ServiceTime") = strServiceTime
            End If
            Return ViewState("ServiceTime")
        End Get
        Set(ByVal value As String)
            ViewState("ServiceTime") = value
        End Set
    End Property

    Public Property WorkTimeDT() As Data.DataTable
        Get
            Return ViewState("WorkTimeDT")
        End Get
        Set(ByVal value As Data.DataTable)
            ViewState("WorkTimeDT") = value
        End Set
    End Property

    Public Property VBasicDataDT() As Data.DataTable
        Get
            Return ViewState("VBasicDataDT")
        End Get
        Set(ByVal value As Data.DataTable)
            ViewState("VBasicDataDT") = value
        End Set
    End Property

    Public Property Region1ID() As String
        Get
            Return ViewState("Region1ID")
        End Get
        Set(ByVal value As String)
            ViewState("Region1ID") = value
        End Set
    End Property

    Public Property Region2ID() As String
        Get
            Return ViewState("Region2ID")
        End Get
        Set(ByVal value As String)
            ViewState("Region2ID") = value
        End Set
    End Property

    Public Property OrganizationID() As String
        Get
            Return ViewState("OrganizationID")
        End Get
        Set(ByVal value As String)
            ViewState("OrganizationID") = value
        End Set
    End Property
    Public Property VOrganizationID() As String
        Get
            Return ViewState("VOrganizationID")
        End Get
        Set(ByVal value As String)
            ViewState("VOrganizationID") = value
        End Set
    End Property

    Public Property VolunteerName() As String
        Get
            Return ViewState("VolunteerName")
        End Get
        Set(ByVal value As String)
            ViewState("VolunteerName") = value
        End Set
    End Property

    Public Property VolunteerID() As String
        Get
            Return ViewState("VolunteerID")
        End Get
        Set(ByVal value As String)
            ViewState("VolunteerID") = value
        End Set
    End Property
#End Region

    

End Class
